package org.shj.basic


object ArrayDemo {
  def main(args: Array[String]): Unit = {
    var array = Array.ofDim[Int](3,3)  //创建一个二维数组
    
    for(i <- 0 to array.length - 1){  //包含 to 后面的值
      for(j <- 0 to array.length - 1){
        array(i)(j) = i + j
      }
    }
    
    //打印二维数组中的值
    for(i <- 0 to array.length - 1){
      for(j <- 0 to array.length - 1){
        print(array(i)(j) + " ")
      }
      println
    }
    
    println("Array.range(10, 20, 2): ")
    var arr = Array.range(10, 20, 2) //2是步长, 不包含 20
    arr.foreach(x => print(x + " "))    
    println
    
    
    arr = Array.apply(5,2,4,3,1)
    arr(0) = 10
    print("替换后Array的值： ")
    for ( item <- arr) 
      print(item + ", ")
    println
    
    scala.util.Sorting.quickSort(arr)
    println("after quick sort:" + arr.mkString(", "))
    
    //在原来的Array的基础上，对每个元素进行操作，获得一个新元素
    val arrDouble = for(item <- arr) yield item * 2
    println("arrDouble: " + arrDouble.mkString(", "))
    
    println("filter(): " + arr.filter(x => x % 2 == 0).mkString(" "))
    println("map(): " + arr.map( _ * 10).mkString(" "))
    
     
    import scala.collection.mutable.ArrayBuffer
    val ab = ArrayBuffer[Int]()
    ab += 1
    ab += 2
    ab += (3,8,7,6)
    
    ab ++= Array(9,4,5)
    println(ab)
    
    print("反向打印： ")
    for(i <- (0 until ab.length).reverse)
      print(ab(i) + " ")
    println
    
    print("Array.tabulate(4)(i => i * 2): " )
    val tmp = Array.tabulate(4)(i => i * 2)
    tmp.foreach(item => print(item + ", "))
    println
    
    println("=========== 测试过过滤后为空，继续操作")
    val a = Array(1,2,3,4,5)
    println(a.filter(_ > 5).map(_*2).sum)
    
  }
}